import { defineStore } from 'pinia'

export const useCommonStore = defineStore(
  'common',
  () => {
    const token = ref<string | null>(null)
    const residentId = ref<string | null>(null)
    const residentType = ref<string | null>(null)
    const totalAlarmsData = ref<any>([]) // 告警信息用于首页左侧区域展示
    const loading = ref<boolean>(false) // 全局的loading动画

    // 设置loading
    const setLoading = (flag: boolean) => {
      loading.value = flag
    }

    // 设置token
    const setToken = (tokenInfo: string) => {
      token.value = tokenInfo
    }

    // 设置residentId
    const setResidentId = (residentIdInfo: string | null) => {
      residentId.value = residentIdInfo
    }

    // 设置residentType
    const setResidentType = (setResidentType: string | null) => {
      residentType.value = setResidentType
    }

    // 设置告警信息
    const setTotalAlarmsData = (setTotalData: any) => {
      totalAlarmsData.value = setTotalData
    }

    return {
      token,
      setToken,
      residentId,
      setResidentId,
      residentType,
      setResidentType,
      totalAlarmsData,
      setTotalAlarmsData,
      setLoading,
      loading
    }
  },
  {
    // 持久化配置
    // 默认开启存储
    // persist: true,
    // 可进行单独配置，优先级高于main.ts中的配置
    persist: [
      {
        paths: ['token'],
        storage: localStorage
      },
      {
        paths: ['residentId'],
        storage: sessionStorage
      },
      {
        paths: ['totalAlarmsData'],
        storage: sessionStorage
      }
    ]
  }
)
